with Ada.Containers; use Ada.Containers;

with Ada.Assertions; use Ada.Assertions;

with GNAT.IO;
use GNAT;

--
-- test package supplying utility for reading file into a bounded string
--

package body Test_Text_File_Reader is

   --
   -- run all tests
   --

   procedure Tests_Suite is
   begin

      Get_List_Test_File_Names;

      Test_Read_Into_String;


   end Tests_Suite;


   --
   -- define equality test for bounded string
   --
   --    necessary for storage of test file names inside a vector collection
   --

   function "=" ( This : in File_Name_Bound_Sub_Type;
                  That : in File_Name_Bound_Sub_Type )

                 return Boolean
   is

   begin

      return File_Name_Bound_Type.To_String(This) =
             File_Name_Bound_Type.To_String(That);

   end "=";


   --
   -- test read of file into string, preserving any /n or /r
   --

   procedure Test_Read_Into_String is

      procedure Test_Read_Strategy( File_Name : in File_Name_Type )
      is

      begin

         IO.Put_Line( Ascii.LF
                      & "File read, Text IO library for: "
                      & File_Name
                      & Ascii.LF
                      & "Length: "
                      & Positive'Image(

                                 File_Util.Read_Into_String( File_Name )

                               'Length ));


         File_Util.Read_Into_String( File_Name );

         IO.Put_Line( Ascii.LF
                      & "File read, Sequential IO library for: "
                      & File_Name
                      & Ascii.LF
                      & "Length: "
                      & Positive'Image(

                        File_Util.Large_File_Contents_String_Type

                                 .Length( File_Util.Large_File_Contents )));

         --
         -- define exception handlers, gracefully handle files that are too
         --    large for given file reader utility method in use
         --

      exception

         when Storage_Error =>

            GNAT.IO.Put_Line( "Error: File is too large" );

            return;

      end Test_Read_Strategy;

      File_Name : File_Name_Bound_Sub_Type;

   begin

      File_Name_Bound_Type.Append( File_Name,
                                   "test_encyclopedia.txt" );

      Assert( Test_Files_Vector.Contains( File_Name ), "Did not find "
                & "'test_encyclopedia.txt' in collection of test file names" );

      --
      -- iterate collection of test file names, using read utility methods
      --

      for File_Number in Test_Files_Vector.First_Index ..

                         Test_Files_Vector.Last_Index
      loop

         declare

            File_Name_From_Collection : constant String :=

              File_Name_Bound_Type.To_String(

              Test_Files_Vector.Element( File_Number ));

         begin

            GNAT.IO.Put_Line( Ascii.LF
                              & "Read tests using file: "
                              & File_Name_From_Collection );

            Test_Read_Strategy( File_Name_From_Collection );

         end;

      end loop;

   end Test_Read_Into_String;


   --
   -- populate vector collection list of test file names
   --

   procedure Get_List_Test_File_Names
   is

      Missing_Test_File_Names : exception;

   begin

      Text_IO.Open( File => File_Names_Listing_Input,
                    Mode => Text_IO.In_File,
                    Name => FILE_NAMES_LISTING );

      while not Text_IO.End_Of_File( File_Names_Listing_Input ) loop

         declare

            Line_In : constant String := Text_IO.Get_Line(

                  File => File_Names_Listing_Input );

            File_Name_As_Bound_Str : File_Name_Bound_Sub_Type;

            Line_Position : constant Positive := Positive(

                  Text_IO.Line( File_Names_Listing_Input )) - 1;

         begin

            File_Name_Bound_Type.Append( File_Name_As_Bound_Str, Line_In );

            Test_Files_Vector_Type.Insert(
                  Container => Test_Files_Vector,
                  Before => Line_Position,
                  New_Item => File_Name_As_Bound_Str );

         end;

      end loop;

      if( Test_Files_Vector.Length < COUNT_TEST_FILES_FOR_READER ) then

         raise Missing_Test_File_Names;

      end if;

   end Get_List_Test_File_Names;


end Test_Text_File_Reader;